package ConveyorBelt.Level;

import java.util.Iterator;
import java.util.Vector;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.BasicGameState;

import ConveyorBelt.Base.Entity;
import ConveyorBelt.Base.Level;
import ConveyorBelt.Base.Score;
import ConveyorBelt.Base.Entity.Role;
import ConveyorBelt.Map.Map;
import ConveyorBelt.Util.Clock;
import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;

public class CurrentLevel implements Level {
    public Vector<Entity> entities;
    private Map map;
    private boolean levelWon = false;
    private boolean levelLose = false;
    private static Clock clock;
    public static int levelNumber;
    public int spawnTime;
    public static int destinationCount;
    /**
     * Constructor của một SimpleLevel
     * @param mapPath Đường dẫn đến map tương ứng, được lưu trong HashTable
     */
    public CurrentLevel(String mapPath, int levelNumber) {
    	setLevelNumber(levelNumber);
		entities = new Vector<Entity>();
		try {
		    map = new Map(mapPath);
		} catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
		map.addToLevel(this);
		clock = new Clock(levelNumber);
		destinationCount = countDestination();
    }
	
    public void render(BasicGameState bgs, Graphics g) {
    	ImageUtil.gameDialog.get(0).draw(220, 0, 840, 680);
//    	System.out.println("Queue Size: " + Map.dummyQueue.size() + " Time: " + spawnTime);
       	if(spawnTime >= 6000 && Map.dummyQueue.size() > 0) {
       		spawnTime = 0;
       		add(Map.dummyQueue.poll());
    	}
		for (Entity e : entities) {
		    e.render(bgs, g);
		}
		g.setFont(FontUtil.uf);
		ImageUtil.roundBox.draw(0, 0, ImageUtil.roundBox.getWidth(), ImageUtil.roundBox.getHeight() + 25);
		g.drawString("Level: " + getLevelNumber(), 10, 10);						
		g.drawString("Score: " + Score.getCurrentScore(), 10, 40);
		g.drawString("Time : " + Clock.getTime(), 10, 70);
		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 225, 675);
    }
    
    public void update(GameContainer gc, int delta) {
    	spawnTime += delta;
		for (Iterator<Entity> iterator = entities.iterator(); iterator.hasNext();) {
		    Entity ent = iterator.next();
		    ent.update(gc, delta);
		}
		removeEntities();
		isLevelWon();
		isLevelLose();
		clock.update(delta);
    }
    public void add(Entity e) {
		entities.add(e);
    }

    public void clear() {
    	for (int i = 0; i < entities.size(); i++) {
		    entities.get(i).removeFromLevel(this);
		    entities.remove(i);
		}
    }

    public void remove(Entity e) {
    	entities.remove(e);
    }  
    
    public boolean isLevelWon() {
    	if((Clock.getTimer() != 0) && Map.dummyQueue.size() == 0 && !haveADummyOnMap()){
    		Score.setFinalScore(Score.getCurrentScore());
    		levelWon = true;
    	}														
    	return levelWon;												
    }

    public boolean isLevelLose() {
    	if((Clock.getTimer() == 0)) {
    		Score.setFinalScore(Score.getCurrentScore());
 
    		levelLose = true;
    	}
    	return levelLose;														
    }
    public boolean haveADummyOnMap() {
    	for (int i = 0; i < entities.size(); i++) {
		    if(entities.get(i).getRole() == Role.DUMMY) return true;
		}
    	return false;
    }
    public int countDestination() {
    	int count = 0;
    	for (int i = 0; i < entities.size(); i++) {
		    if(entities.get(i).getRole() == Role.DESTINATION) count++;
		}
    	return count;
    }
    private void removeEntities() {
		Vector<Entity> removeEnts = new Vector<Entity>();
		for(int i=0; i<entities.size(); i++) {
			Entity ent = entities.get(i);
			if(ent.isToRemove()) removeEnts.add(ent);
		}
		entities.removeAll(removeEnts);
    }

	public int getLevelNumber() {
		return levelNumber;
	}

	public void setLevelNumber(int levelNumber) {
		CurrentLevel.levelNumber = levelNumber;
	}
}
